package com.wandian.mvc
{
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;



	public class Facade
	{
		private var name:String
		private var dispatcher:Dispatcher

		private var initialized:Boolean=false;
		private var started:Boolean=false;
		private var proxyMap:Dictionary
		//中介者集合
		private var mediatorMap:Dictionary
		private var currentStarted:Mediator
		//当前精灵
		private var contentView:Sprite

		public function Facade(name:String)
		{
			name=name;
		}


		public function getContentView():Sprite
		{
			return contentView;
		}

		/**
		 * 设置当前显示精灵
		 * @param contentView
		 */
		public function setContentView(contentView:Sprite):void
		{
			this.contentView=contentView;
		}


		public function notify(name:String, data:Object):void
		{

		}

		//注册中介者，同时注册了所包含的所有事件
		protected function registerMediator(mediator:Mediator):void
		{
			var key:String=mediator.name
			if (mediatorMap[key] != null)
			{
				return
			}
			mediatorMap[key]=mediator
			mediator.facade=this;
			var array:Array=mediator.interests
			for (var i:int=0; i < array.length; i++)
			{
				addListener(array[i], mediator.ActionHandler)
			}
		}

		protected function removeMediator(mediator:Mediator):void
		{
			var key:String=mediator.name
			var array:Array=mediator.interests
			for (var i:int=0; i < array.length; i++)
			{
				removeListener(array[i], mediator.ActionHandler)
			}
			mediatorMap[key]=null
		}

		protected function registerProxy(proxy:Proxy):void
		{
			var key:String=proxy.name
			if (proxyMap[key] != null)
			{
				return
			}
			proxyMap[key]=proxy;
			proxy.addFacade(this);
		}

		protected function removeProxy(proxy:Proxy):void
		{
			var key:String=proxy.name
			proxy.removeFacade(this)
			proxyMap[key]=null;

		}

		internal function dispatchAction(target:Object, type:String, body:Object):void
		{
			trace("MVC : " + target + " >>> " + type);
			dispatcher.dispatchEvent(new Action(target, type, body));
		}

		internal function dispatchActionForResult(target:Object, type:String, body:Object):Object
		{
			trace("MVC : " + target + " >>> " + type);
			var action:ActionForResult=new ActionForResult(target, type, body)
			dispatcher.dispatchEvent(action);
			return action.getResult();
		}

		internal function addListener(type:String, listener:Function):void
		{
			dispatcher.addEventListener(type, listener);
		}

		internal function removeListener(type:String, listener:Function):void
		{
			dispatcher.removeEventListener(type, listener);
		}

		//启动观察者，在子类重写初始化
		public function start(... args):void
		{
			if (!initialized)
			{
				initialized=true;
				dispatcher=new Dispatcher();
				mediatorMap=new Dictionary(true);
				proxyMap=new Dictionary(true);
				onInitialize()
			}
			if (!started)
			{
				started=true;
				onStart(args);
			}

		}

		protected function onStop():void
		{

		}

		protected function onStart(args:Array):void
		{

		}

		protected function onInitialize():void
		{

		}

		public function stop():void
		{
			if (!started)
			{
				return
			}
			started=false;
			onStop();
		}

	}
}
import flash.events.EventDispatcher;

class Dispatcher extends EventDispatcher
{

}
